﻿using System.Runtime.InteropServices;

namespace BoxPcTool
{
  public static class WakeSleepHelper
  {
    [DllImport("kernel32.dll")]
    private static extern uint SetThreadExecutionState(uint esFlags);

    private const uint ES_SYSTEM_REQUIRED = 0x00000001;
    private const uint ES_DISPLAY_REQUIRED = 0x00000002;
    private const uint ES_CONTINUOUS = 0x80000000;

    public static void WakeSleep(bool sleepOrNot)
    {
      if (sleepOrNot)
      {
        // 阻止休眠时调用，这里不阻止屏幕关闭。
        // SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED);
        SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
      }
      else
      {
        //恢复休眠时调用
        SetThreadExecutionState(ES_CONTINUOUS);
      }
    }
  }
}